<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/******************************************************
* Profile			: article 
* Author			: 宁秀娟
* Create Time		: 2012-05-30
* Modify Time		: 2012-05-30
* Modify Profile	:  
******************************************************/
class usergroup extends Admin_Controller {

	/******************************************************
	 * Profile			: 列表
	* Author			: 宁秀娟
	* Create Time		: 2012-05-30
	* Modify Time		: 2012-05-30
	* Modify Profile	:
	******************************************************/
	public function index()
	{
		$usergroup = $this->usergroup_manager->get(2);
		/* 
		if($usergroup->permission("menu","order"))
		{
			echo "yes";
		} 
		else
		{
			echo "no";
		}
		exit; 
		*/  
		$usergroup = new usergroup_model();
		// print_r($usergroup);exit;
		if(isset($_GET['id']))
		{
			$usergroup = $this->usergroup_manager->get($this->input->get('id')); 
		}
		if($this->is_postback())
		{
			$this->load->library('form_validation');
	
			if ($this->form_validation->run('usergroup') == TRUE)
			{
				$post_usergroup = $this->filter_post();
				$id = $this->input->get('id');
				if(empty($id))
				{
					$id = $this->usergroup_manager->insert($post_usergroup);
				}
				else
				{
					$this->usergroup_manager->update($post_usergroup,array('id'=>$id)) ; 
				}
				redirect(SELF."/usergroup/index?id=$id") ;
			}
		}
		$usergroupes = $this->usergroup_manager->get_root_list();
		$usergroup->usergroupes = $usergroupes ;
		// print_r($usergroup);exit;
		
		$usergroup->part_templates =  $this->part_templates(); 
		$this->load->view("admin/usergroup/v_index",$usergroup);
	}
	
	
	function check_groupname($group_name)
	{
		$usergroup = null;
	
		if(isset($_GET['id']))
		{
			$usergroup = $this->usergroup_manager->get(
				array(
					'name'=>$group_name,
					'id !='=>$this->input->get('id')
				)
			);
		}
		else
		{
			$usergroup = $this->usergroup_manager->get(array(
				'name'=>$group_name
			));
		}
		// print_r($usergroup);exit;
		if(!empty($usergroup->id)) 
		{
			$this->form_validation->set_message('check_groupname', '用户组名不能重复');
			return false;
		}
		return true;
	}
	
	
	private function part_templates()
	{
		$article_view_folder = APPPATH."views/".$this->template."/front/article";
		// echo $page_view_folder;exit ;
		$d = dir($article_view_folder);
		$part_templates = Array();
		while (false !== ($f = $d->read()))
		{
			if(preg_match_all("/^v_(.*?)_index.php$/i", $f,$m))
			{
				$part_templates[] =  $m[1][0];
			}
		}
		return $part_templates;
	}
	
	/******************************************************
	* Profile		: 编辑
	* Author		: 宁秀娟
	* Create Time		: 2012-05-30
	* Modify Time		: 2012-05-30
	* Modify Profile	:  
	******************************************************/
	public function edit($id = null)
	{
		$id = ($id == null)?$this->input->get("id"):$id;
		
		if($id == null) // 添加
		{
			$usergroup = new usergroup_model(); 
		}
		else
		{
			$usergroup = $this->usergroup_manager->get($id); 
		}
			
		if($this->is_postback())
		{
			$this->load->library('form_validation');
	
			if ($this->form_validation->run('usergroup') == TRUE)
			{
				$usergroup = $this->filter_post($_POST); 

				$str_acls = $this->input->post('acls');
				$acls = array(); 
				foreach($str_acls as $str_acl)
				{
					$tmps = explode("~", $str_acl);
					$acl = new stdClass();
					$acl->resource = $tmps[0];
					$acl->action = $tmps[1]; 
					$acls[] = $acl;
				}
				$usergroup["acls"] = $acls; 
				// print_r($usergroup);exit;
	
				if($id == null)  // 添加
				{
					// $usergroup['contribute_time'] = Date('Y-m-d');  
					$this->usergroup_manager->insert($usergroup);  
				}
				else 
				{
					$this->usergroup_manager->update($usergroup,array('id'=>$id));
				}
				if(!empty($_GET['returnurl']))
				{
					$this->goto_url(urldecode($this->input->get('returnurl'))) ;
					exit;
				}
				$this->goto_last_index_page();
			}
		}
		$usergroup->part_templates =  $this->part_templates();
		// print_r($usergroup);exit; 
		$this->load->view("admin/usergroup/v_form",$usergroup);
	}


	/******************************************************
	* Profile		: 删除
	* Author		: 宁秀娟
	* Create Time		: 2012-05-30
	* Modify Time		: 2012-05-30
	* Modify Profile	:  
	******************************************************/
	public function del($id = null) 
	{
		//$id = isset($_GET['id'])?$_GET['id']:$id;
		//$this->usergroup_manager->delete($id); 
		//$this->goto_last_index_page();
		
		$id = isset($_GET['id'])?$_GET['id']:$id;
		if(!empty($id) && $id!=1)
		{
			$this->usergroup_manager->delete($id) ;
		}
		// print_r($_POST);exit;
		if(isset($_POST['ids']))
		{
			//echo implode(',', $this->input->post('ids')); exit ;
			foreach($this->input->post('ids') as $id)
			{
				if( $id!=1)
				{
					$this->usergroup_manager->delete($id) ;
				}
			}
		}
		$this->goto_last_index_page();
	}
	
	public function get_list_for_ztree()
	{
		$id = isset($_REQUEST['id'])?$_REQUEST['id']:0;
		//echo $id;exit;
		//$lists = "{id:'',name:'No Data',isParent:'false'}";
		$lists = Array();
		if($id==0)
		{ 
			$lists = $this->usergroup_manager->get_root_list();
		}
		else
		{
			$lists = $this->usergroup_manager->get($id)->children;
		}
		
		$list_jsons = array();
		foreach ($lists as $acls)
		{
			$isParent = sizeof($acls->children)==0?'false':'true';
			$list_jsons[] = "{id:'$acls->id',name:'$acls->title',isParent:'$isParent'}" ;
		}
		
		if(sizeof($list_jsons)==0)
		{
			echo "no data";
		}
		else
		{
			echo "[".implode(',',$list_jsons)."]";
		}
	}
	
	/******************************************************
	 * Profile		: 排序
	* Author		: 宁秀娟
	* Create Time		: 2012-05-30
	* Modify Time		: 2012-05-30
	* Modify Profile	:
	******************************************************/
	public function order()
	{
		// print_r($_POST['listorder']);exit;
		if(isset($_POST['listorder']) && is_array($_POST['listorder']))
		{
			foreach($this->input->post('listorder') as $k=>$v)
			{
				$this->usergroup_manager->update(array('order'=>$v),array('id'=>$k)) ; 
			}
		}
		$this->goto_last_index_page();
	}
}

